package com.zz.impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Expression;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.zz.dao.ReadRoomsDao;
import com.zz.dao.StatisticsDao;
import com.zz.entity.DateUtil;
import com.zz.entity.ReadRooms;
import com.zz.entity.Statistics;
import com.zz.entity.StatisticsSearch;
import com.zz.service.StatisticsService;

@Service
public class StatisticsImpl implements StatisticsService {
	@Autowired
	StatisticsDao sdao;
	@Autowired
	ReadRoomsDao rdao;

	public List<Statistics> GetAll1() {
		return sdao.findAll();
	}

	public Map<String, Object> GetAll(StatisticsSearch staSearch, Integer page, Integer size) {
		if(page == 0) page=page+1;
		Pageable p = PageRequest.of(page-1, size);
		Specification<Statistics> spec = getSpec(staSearch);
		Page<Statistics> pagelist = sdao.findAll(spec,p);
		Integer count = (int) pagelist.getTotalElements();
		Map<String, Object>map=new HashMap<String, Object>();
		map.put("code", 0);
		map.put("msg", "");
		map.put("data", pagelist.getContent());
		map.put("count", count);
		return map;
	}

	private Specification<Statistics> getSpec(StatisticsSearch staSearch) {
		return new Specification<Statistics>() {
			
			private static final long serialVersionUID = 1L;

			public Predicate toPredicate(Root<Statistics> root, CriteriaQuery<?> query, CriteriaBuilder criteriaBuilder) {
				Predicate pre = criteriaBuilder.conjunction();
				List<Expression<Boolean>> list = pre.getExpressions();
				if(!StringUtils.isEmpty(staSearch.getReadRoomId())) {
					list.add(criteriaBuilder.equal(root.get("ReadRoomId"), staSearch.getReadRoomId()));
				}
				if(!StringUtils.isEmpty(staSearch.getMaxPeopleNums())) {
					list.add(criteriaBuilder.equal(root.get("PeopleNums"), staSearch.getMaxPeopleNums()));
				}
				return pre;
			}
		};
	}

	//查询某个资源室某个时间段的人数总和
	public Integer GetPeppleNumsByDate(Integer id, String begin, String end) {
		return sdao.getPeoplenumsByDate(id, begin, end);
	}

	//查询所有资源室的某时间段的人数和
	public List<Map<String, Integer>> GetStatisticsPeopleNums(String begin, String end) {
		if(begin==null)begin="2000-01-01";
		if(end==null)end=new DateUtil().getNewDate();
		return sdao.getStatisticsPeopleNums(begin, end);
	}
	
	public Object getTJReadRoom(String begintime, String endTime) {
		List<String> rooms=new ArrayList<String>();
		List<ReadRooms> readrooms=rdao.findAll();
		System.out.println(readrooms);
		for (int i = 0; i < readrooms.size(); i++) {
			rooms.add(i, readrooms.get(i).getName().toString());
		}
		Map<String, Object> map1 = new HashMap<String, Object>();
		map1.put("readrooms", rooms);
		if(begintime==null)begintime="2000-01-01";
		if(endTime==null)endTime=new DateUtil().getNewDate();
		List<Integer> readIds=new ArrayList<Integer>();
		for (int i = 0; i < rooms.size(); i++) {
			Integer readId=rdao.getcardid(rooms.get(i));
			readIds.add(i, readId);
		}
		List<Integer> o1=new ArrayList<Integer>();
		System.out.println(o1);
		for (int i = 0; i < readIds.size(); i++) {
			Integer peonum=sdao.getPeoplenumsByDate(readIds.get(i),begintime,endTime);
			if(peonum==null)peonum=0;
			o1.add(peonum);
		}
		map1.put("peonum", o1);
		return map1;
	}

}
